package cn.wolfcode.trip.admin.web.controller;

import cn.wolfcode.trip.admin.util.UploadUtil;
import cn.wolfcode.trip.base.domain.ViewPoint;
import cn.wolfcode.trip.base.query.ViewPointQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.service.IViewPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/viewpoint")
public class ViewPointController {

    private final IViewPointService viewPointService;
    private final IRegionService regionService;

    @Autowired
    public ViewPointController(IViewPointService viewPointService, IRegionService regionService) {
        this.viewPointService = viewPointService;
        this.regionService = regionService;
    }

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") ViewPointQueryObject qo, Model model) {
        //设置按照最后更新时间升序排列
        model.addAttribute("pageInfo", viewPointService.queryViewPoints(qo));
        model.addAttribute("places", regionService.list());
        return "viewPoint/list";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(ViewPoint entity, MultipartFile img) {
        if (img != null && img.getContentType().startsWith("image") && img.getSize() > 0) {
            String url = UploadUtil.uploadQiniu(img);
            entity.setCoverUrl(url);
        }
//        System.out.println(entity);
        viewPointService.saveOrUpdate(entity);
        return "redirect:/viewpoint/list.do";
    }
}
